package com.atguigu.tingshu.album.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.album.mapper.*;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.model.album.BaseAttribute;
import com.atguigu.tingshu.model.album.BaseCategory1;
import com.atguigu.tingshu.model.album.BaseCategoryView;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.gson.JsonObject;
import netscape.javascript.JSObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@SuppressWarnings({"all"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

	@Autowired
	private BaseCategory1Mapper baseCategory1Mapper;

	@Autowired
	private BaseCategory2Mapper baseCategory2Mapper;

	@Autowired
	private BaseCategory3Mapper baseCategory3Mapper;

	@Autowired
	private BaseCategoryViewMapper baseCategoryViewMapper;

	@Autowired
	private BaseAttributeMapper baseAttributeMapper;


	//查询所有分类（1、2、3级分类）
	@Override
	public List<JSONObject> getBaseCategoryList() {
		//创建封装一级分类的容器
		ArrayList<JSONObject> returnList = new ArrayList<>();
		//查询视图所有分类
		List<BaseCategoryView> allCategoryList = baseCategoryViewMapper.selectList(null);
		//对一级分类进行分组
		Map<Long, List<BaseCategoryView>> catergoryMap1 = allCategoryList.stream()
				.collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));
		//遍历分组对象
		for (Map.Entry<Long, List<BaseCategoryView>> entry1 : catergoryMap1.entrySet()) {

			//封装对象
			JSONObject jsonObject1 = new JSONObject();
			//获取分类ID
			Long entry1Key = entry1.getKey();
			jsonObject1.put("categoryId",entry1Key);
			//获取一级分类名称
			String category1Name = entry1.getValue().get(0).getCategory1Name();
			jsonObject1.put("categoryName",category1Name);

			//存入容器
			returnList.add(jsonObject1);

			//处理二级
			//创建容器
			ArrayList<JSONObject> category1Child= new ArrayList<>();
			//根据一级进行分组
			Map<Long, List<BaseCategoryView>> catergoryMap2 = entry1.getValue().stream()
					.collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
			//遍历
			for (Map.Entry<Long, List<BaseCategoryView>> entry2 : catergoryMap2.entrySet()) {
				//封装对象
				JSONObject jsonObject2 = new JSONObject();
				//获取分类ID
				Long entry2Key = entry2.getKey();
				jsonObject2.put("categoryId",entry2Key);
				//获取分类名称
				String category2Name = entry2.getValue().get(0).getCategory2Name();
				jsonObject2.put("categoryName",category2Name);
				//存入容器
				category1Child.add(jsonObject2);

				//处理三级
				//创建容器
				ArrayList<JSONObject> category2Child = new ArrayList<>();
				//根据entry2遍历
				for (BaseCategoryView baseCategoryView : entry2.getValue()) {
					//封装对象
					JSONObject jsonObject3 = new JSONObject();
					//获取ID
					jsonObject3.put("categoryId",baseCategoryView.getCategory3Id());
					//获取名称
					jsonObject3.put("categoryName",baseCategoryView.getCategory3Name());

					//存入容器
					category2Child.add(jsonObject3);
				}

				// 将3级分类对象集合加入到二级分类对象"jsonObject2"属性中
				jsonObject2.put("categoryChild",category2Child);
			}
			// 将二级分类集合封装在一级分类对象中"jsonObject1"属性中
			jsonObject1.put("categoryChild",category1Child);
			returnList.add(jsonObject1);
		}

		return returnList;
	}

	//根据一级分类Id获取分类属性以及属性值（标签名，标签值）列表
	@Override
	public List<BaseAttribute> getAttributesByCategory1Id(Long category1Id) {
		//调用持久层的方法查询一级分类ID获取分类属性以及属性值（标签名，标签值）列表
		return baseAttributeMapper.getAttributesByCategory1Id(category1Id);
	}
}
